// PageLoginInfo.cpp : implementation file
//

#include "stdafx.h"
#include "finance.h"
#include "financedef.h"
#include "CryptString.h"
#include "PageLoginInfo.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif


// Select to change : m_iSel
/*************
	0,	// Username default
	1	// Password
*************/


/////////////////////////////////////////////////////////////////////////////
// CPageLoginInfo property page

IMPLEMENT_DYNCREATE(CPageLoginInfo, CPropertyPage)

CPageLoginInfo::CPageLoginInfo() : CPropertyPage(CPageLoginInfo::IDD)
{
	//{{AFX_DATA_INIT(CPageLoginInfo)
		// NOTE: the ClassWizard will add member initialization here
	//}}AFX_DATA_INIT
}

CPageLoginInfo::~CPageLoginInfo()
{
}

void CPageLoginInfo::DoDataExchange(CDataExchange* pDX)
{
	CPropertyPage::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CPageLoginInfo)
	DDX_Control(pDX, IDC_ED_OLD, m_edOld);
	DDX_Control(pDX, IDC_ED_NEW, m_edNew);
	DDX_Control(pDX, IDC_ED_NEW2, m_edNew2);
	//}}AFX_DATA_MAP
}


BEGIN_MESSAGE_MAP(CPageLoginInfo, CPropertyPage)
	//{{AFX_MSG_MAP(CPageLoginInfo)
	ON_BN_CLICKED(IDC_CHK_MODIFY, OnChkModify)
	ON_BN_CLICKED(IDC_CHK_ENABLELOGIN, OnChkEnablelogin)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CPageLoginInfo message handlers

// 
// 
//------------------------------------------------------------
BOOL CPageLoginInfo::OnInitDialog() 
{
	CPropertyPage::OnInitDialog();
	

	CButton			*pBtn = NULL;
	pBtn = (CButton*)GetDlgItem( IDC_CHK_MODIFY );
	pBtn->SetCheck( 0 );

	m_edOld.SetLimitText( PASSWORD_MAXLEN );
	m_edNew.SetLimitText( PASSWORD_MAXLEN );
	m_edNew2.SetLimitText( PASSWORD_MAXLEN );
	
	m_edOld.EnableWindow( FALSE );
	m_edNew.EnableWindow( FALSE );
	m_edNew2.EnableWindow( FALSE );
	

	BYTE	btEnablePop = 1;	// default
	HKEY	hRegKey = NULL;
	// <> get password form Register
	if( RegOpenKeyEx( HKEY_CURRENT_USER, IDS_REGSRBKEY, 0, KEY_QUERY_VALUE, &hRegKey ) == ERROR_SUCCESS ) 
	{		
		DWORD	dwLength = sizeof(btEnablePop);
		DWORD	dwType = REG_BINARY;
		
		if( RegQueryValueEx( hRegKey, _T("Popup"), NULL, &dwType, (BYTE*)&btEnablePop, &dwLength ) != ERROR_SUCCESS )
			btEnablePop = 1;	// default
		
		::RegCloseKey( hRegKey );	
	}
	m_iEnablePop = (int)btEnablePop;

	pBtn = (CButton*)GetDlgItem( IDC_CHK_ENABLELOGIN );
	pBtn->SetCheck( m_iEnablePop );
	

	return TRUE;  // return TRUE unless you set the focus to a control
	              // EXCEPTION: OCX Property Pages should return FALSE
}

//
// 
//------------------------------------------------------------
void CPageLoginInfo::OnOK() 
{
	CButton			*pBtn = (CButton*)GetDlgItem( IDC_CHK_MODIFY );
	HKEY			hRegKey = NULL;
	

	UpdateData( TRUE );

	// Enable/Disable Login dlg when start
	if( RegOpenKeyEx( HKEY_CURRENT_USER, IDS_REGSRBKEY, 0, KEY_WRITE, &hRegKey ) == ERROR_SUCCESS )
	{		
		BYTE	btPop = (BYTE)m_iEnablePop;
		::RegSetValueEx( hRegKey, _T("Popup"), 0, REG_BINARY, &btPop, sizeof(btPop) );
		::RegCloseKey( hRegKey );
	}	

	// <> check password changed
	if( pBtn->GetCheck() == 1 ) //  checked
	{
		CString		csOld, csNew, csNew2;

		m_edOld.GetWindowText( csOld );	
		m_edNew.GetWindowText( csNew );
		m_edNew2.GetWindowText( csNew2 );
		
		if( csOld.IsEmpty() ) 
		{
			MessageBox( "Old password must be input!", "Warning" , MB_ICONWARNING );
			return ;
		}
		if( csNew.IsEmpty() || csNew2.IsEmpty() ) 
		{
			MessageBox( "New password must be input!", "Warning" , MB_ICONWARNING );
			return ;
		}
		if( csNew != csNew2 )
		{
			MessageBox( "New password and confirm must be same!", "Warning" , MB_ICONWARNING );
			return ;
		}

		// Init the crypto class
		CCryptString	oCrypt;
		CHAR			chDecrypted[ 100 ] = { 0 };
		CHAR			chEncrypted[ 100 ] = { 0 };

		
		// <> get password form registry	
		if( RegOpenKeyEx( HKEY_CURRENT_USER, IDS_REGSRBKEY, 0, KEY_QUERY_VALUE, &hRegKey ) == ERROR_SUCCESS ) 
		{		
			DWORD	dwLength = 100*sizeof(CHAR);
			DWORD	dwType = REG_BINARY;
			
			if( RegQueryValueEx( hRegKey, _T("Password"), NULL, &dwType, (BYTE*)chEncrypted, &dwLength ) != ERROR_SUCCESS )
				GetAPDefaultPwd( chEncrypted );	// Use default pwd when error
			
			::RegCloseKey( hRegKey );	
		}
		else
			GetAPDefaultPwd( chEncrypted );	// Use default pwd when error

		// <> decrypt old from ini
		oCrypt.DecryptString( chEncrypted, chDecrypted, IDS_PWDKEY );
		if( csOld != chDecrypted ) 
		{
			MessageBox( "Invalid old password!", "Error" , MB_ICONERROR );
			return ;
		}

		// encrpyt new
		memset( chEncrypted, 0, sizeof(chEncrypted) );
		strcpy( chDecrypted, csNew );
		oCrypt.EncryptString( chDecrypted, chEncrypted, IDS_PWDKEY );

		// save new to registry
		if( RegOpenKeyEx( HKEY_CURRENT_USER, IDS_REGSRBKEY, 0, KEY_WRITE, &hRegKey ) == ERROR_SUCCESS )
		{		
			// <> Add the password.
			::RegSetValueEx( hRegKey, _T("Password"), 0, REG_BINARY, (const unsigned char*)chEncrypted, strlen(chEncrypted) );
			::RegCloseKey( hRegKey );
		}	

	}
		
	CPropertyPage::OnOK();
}

//
// 
//------------------------------------------------------------
void CPageLoginInfo::OnChkModify() 
{
	CButton *pBtn = (CButton*)GetDlgItem( IDC_CHK_MODIFY );
	
	if( pBtn->GetCheck() == 0 ) //  unchecked
	{
		m_edOld.EnableWindow( FALSE );
		m_edNew.EnableWindow( FALSE );
		m_edNew2.EnableWindow( FALSE );
	}
	else if( pBtn->GetCheck() == 1 ) // checked
	{
		m_edOld.EnableWindow( TRUE );
		m_edNew.EnableWindow( TRUE );
		m_edNew2.EnableWindow( TRUE );

		m_edOld.SetFocus();
	}
}

// 
//------------------------------------------------------------
void CPageLoginInfo::OnChkEnablelogin() 
{
	CButton *pBtn = (CButton*)GetDlgItem( IDC_CHK_ENABLELOGIN );
	
	
	if( pBtn->GetCheck() == 0 ) //  unchecked
	{
		m_iEnablePop = 0;
	}
	else if( pBtn->GetCheck() == 1 ) // checked
	{
		m_iEnablePop = 1;
	}
}
